package com.cloudsigns.factorymanager2.project.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudsigns.factorymanager2.BaseActivity;
import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.produce.bean.ProduceProduceOrderBean;
import com.cloudsigns.factorymanager2.project.adapter.ProductionTaskOrderListAdapter;
import com.cloudsigns.factorymanager2.project.iview.IGetProduceOrderListView;
import com.cloudsigns.factorymanager2.project.presenter.GetProductOrderListPresenter;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;

import java.util.ArrayList;
import java.util.List;

public class ProductionTaskOrderListActivity extends BaseActivity implements IGetProduceOrderListView {
    private String TAG = ProductionTaskOrderListActivity.class.getSimpleName();
    private RelativeLayout rl_right;
    private ImageView img_back;
    private TextView tv_title;
    private ListView lv;
    private ProductionTaskOrderListAdapter adapter;
    private List<ProduceProduceOrderBean> oList = new ArrayList<>();
    private GetProductOrderListPresenter getProductOrderListPresenter;

    @Override
    protected void onBaseCreate(Bundle savedInstanceState) {
        setContentView(R.layout.layout_product_task_order_list);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        img_back = findViewById(R.id.img_back);
        rl_right = findViewById(R.id.rl_right);
        tv_title = findViewById(R.id.tv_title);
        lv = findViewById(R.id.lv);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        tv_title.setText("生产任务单");

        getProductOrderListPresenter = new GetProductOrderListPresenter(this);

        getOrderList();

//        designTypeBeanList.add(new DesignTypeBean("类别-级别", false, 0));
//        designTypeBeanList.add(new DesignTypeBean("类别-级别", false, 1));
//        adapter = new ProductionTaskOrderListAdapter(this);
//        adapter.setData(designTypeBeanList);
//        lv.setAdapter(adapter);
        adapter = new ProductionTaskOrderListAdapter(this);
        adapter.setData(oList);
        lv.setAdapter(adapter);
    }

    @Override
    public void setListener() {
        img_back.setOnClickListener(this);
        rl_right.setOnClickListener(this);

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                ProduceProduceOrderBean produceOrderBean = oList.get(position);
                Log.e(TAG, "getOrderId=" + produceOrderBean.getOrderId());
                Intent mIntent = new Intent(ProductionTaskOrderListActivity.this, ProductionTaskOrderListItemActivity.class);
                mIntent.putExtra(ConstantUtil.KEY_1, produceOrderBean);
                startActivityForResult(mIntent, ConstantUtil.START_ACTIVITY_KEY_1);
            }
        });
    }

    @Override
    public void widgetClick(View view) {
        Intent mIntent;
        switch (view.getId()) {
            // 返回
            case R.id.img_back:
                finish();
                break;
            // 添加工单
            case R.id.rl_right:
                mIntent = new Intent(ProductionTaskOrderListActivity.this, ProductionTaskOrderActivity1.class);
                startActivityForResult(mIntent, ConstantUtil.CREATED_ORDER);
                break;
        }
    }

    private void getOrderList() {
        getProductOrderListPresenter.getProdectOrderList();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case ConstantUtil.CREATED_ORDER: //创建工单
                if (data != null) {
                    Boolean update = (Boolean) data.getBooleanExtra(ConstantUtil.KEY_1, false);
                    if (update) {
                        getOrderList();
                    }
                }
                break;
            case ConstantUtil.START_ACTIVITY_KEY_1:
                if (data != null) {
                    Boolean update = (Boolean) data.getBooleanExtra(ConstantUtil.KEY_1, false);
                    if (update) {
                        getOrderList();
                    }
                }
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void getDataSuccessed(List<ProduceProduceOrderBean> getProductOrderList) {
        if (null != getProductOrderList) {
            oList.clear();
            oList.addAll(getProductOrderList);
            adapter.setData(oList);
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void showFailedError(String desc) {
        Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }
}
